package sk.tramtatiri;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.apache.wicket.util.lang.PackageName;

import sk.tramtatiri.pages.users.UserHomePage;

/**
 * Application object for your web application. If you want to run this
 * application without deploying, run the Start class.
 * 
 * @see wicket.myproject.Start#main(String[])
 * 
 * @author Richard Wilkinson - richard.wilkinson@jweekend.com
 * 
 */
public class WicketApplication extends WebApplication {
	
	/**
	 * Constructor
	 */
	public WicketApplication() {
	}

	protected SpringComponentInjector getSpringInjector() {
		return new SpringComponentInjector(this);
	}

	@Override
	protected void init() {
		super.init();
		getSecuritySettings().setAuthorizationStrategy(new AuthStrategy());
		
		mount("/userPages",
				PackageName.forPackage(UserHomePage.class.getPackage()));
		mount("/datainit", PackageName.forPackage(DataInit.class.getPackage()));
		
		addComponentInstantiationListener(getSpringInjector());
		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.wicket.Application#getHomePage()
	 */
	@Override
	public Class<? extends Page> getHomePage() {
		return UserHomePage.class;
	}

	public Session newSession(Request request, Response response) {
		return new MySession(request);
	}

	public String hashPassword(String password) {
		String hashword = null;
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(password.getBytes());
			BigInteger hash = new BigInteger(1, md5.digest());
			hashword = hash.toString(16);

		} catch (NoSuchAlgorithmException nsae) {

		}
		return hashword;
	}
	
}
